﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE

#End Region

Namespace SensePanelButtonControlEx

    Public Class Style1
        Inherits SensePanelBase
        Implements IDisposable

#Region "Fields"

        'General Flags
        Private _disposed As Boolean = False

        'General Properties
        Private _name As String = Nothing

        Private _text As String = Nothing
        Private _left As Integer = Nothing
        Private _top As Integer = Nothing
        Private _width As Integer = Nothing
        Private _height As Integer = Nothing

        Private _buttonEnabled As IImage = Nothing
        Private _buttonPressed As IImage = Nothing

        Private _buttonEnabledLeft As IImage = _getIImageFromResource("button.enabled.gray.left")
        Private _buttonEnabledMiddle As IImage = _getIImageFromResource("button.enabled.gray.middle")
        Private _buttonEnabledRight As IImage = _getIImageFromResource("button.enabled.gray.right")

        Private _buttonPressedLeft As IImage = _getIImageFromResource("button.pressed.black.left")
        Private _buttonPressedMiddle As IImage = _getIImageFromResource("button.pressed.black.middle")
        Private _buttonPressedRight As IImage = _getIImageFromResource("button.pressed.black.right")

        Private _clickedButton As Boolean = False
        Private _myCurrentPosition As Integer = 0

#End Region

#Region "Events"

        Public Shadows Event OnClick(ByVal sender As Object)

#End Region

#Region "Methods"

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New()

            Try

                'Do Nothing

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New(ByVal Name As String)

            Try

                'Save name property
                _name = Name

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Dispose
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Overrides Sub Dispose()

            Try

                Me._dispose(True)
                GC.SuppressFinalize(Me)
                MyBase.Dispose()

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' _Dispose
        ''' </summary>
        ''' <param name="disposing"></param>
        ''' <remarks>None</remarks>
        Private Sub _dispose(ByVal disposing As Boolean)

            Try

                Me._disposed = True

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try
        End Sub

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        Public Overrides Sub OnMouseDown(ByVal x As Integer, ByVal y As Integer, ByRef cancelScroll As Boolean)
            MyBase.OnMouseDown(x, y, cancelScroll)

            'Record current control position Y
            _myCurrentPosition = y

            _clickedButton = True

        End Sub

        Public Overrides Sub OnMouseUp(ByVal x As Integer, ByVal y As Integer)
            MyBase.OnMouseUp(x, y)

            'Check controls current position
            If Not y = _myCurrentPosition Then

                _clickedButton = False

                'Then exit sub to avoid raising other events
                Exit Sub

            End If

            _clickedButton = False

            RaiseEvent OnClick(Me)

        End Sub

        ''' <summary>
        ''' OnRender
        ''' </summary>
        ''' <param name="g"></param>
        ''' <remarks></remarks>
        Public Overrides Sub OnRender(ByVal g As System.Drawing.Graphics)
            MyBase.OnRender(g)


            If Not _buttonEnabled Is Nothing Then

                Dim __button As IImage = If(_clickedButton, _buttonPressed, _buttonEnabled)

                g.DrawImageAlphaChannel(__button, New Rectangle(_left, ClientRectangle.Top, _width, ClientRectangle.Height))

            Else

                If _clickedButton Then

                    g.DrawImageAlphaChannel(_buttonPressedLeft, New Rectangle(_left, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Height))
                    g.DrawImageAlphaChannel(_buttonPressedMiddle, New Rectangle(_left + SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width, ClientRectangle.Top, _width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedMiddle).Height))
                    g.DrawImageAlphaChannel(_buttonPressedRight, New Rectangle(_width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Height))

                Else

                    g.DrawImageAlphaChannel(_buttonEnabledLeft, New Rectangle(_left, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Height))
                    g.DrawImageAlphaChannel(_buttonEnabledMiddle, New Rectangle(_left + SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width, ClientRectangle.Top, _width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledMiddle).Height))
                    g.DrawImageAlphaChannel(_buttonEnabledRight, New Rectangle(_width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Height))

                End If

            End If

            ' draw the primary text
            Using f As Font = New Font("Helvetica Neue OTS", 10.0F, FontStyle.Regular)
                Dim s As String = _text
                Dim r As Rectangle = New Rectangle(_left, ClientRectangle.Top, _width, ClientRectangle.Height)
                Using b As Brush = New SolidBrush(If(_clickedButton, Color.White, Color.Black))
                    Using sf As StringFormat = New StringFormat()
                        sf.Alignment = StringAlignment.Center
                        sf.LineAlignment = StringAlignment.Center
                        g.DrawString(s, f, b, r, sf)
                    End Using
                End Using
            End Using

        End Sub

#End Region

#Region "Properties"

        ''' <summary>
        ''' Text
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shadows Property Text() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property

        ''' <summary>
        ''' ButtonEnabledImage
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property ButtonEnabledImage() As IImage
            Get
                Return _buttonEnabled
            End Get
            Set(ByVal value As IImage)
                _buttonEnabled = value
            End Set
        End Property

        ''' <summary>
        ''' ButtonPressedImage
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property ButtonPressedImage() As IImage
            Get
                Return _buttonPressed
            End Get
            Set(ByVal value As IImage)
                _buttonPressed = value
            End Set
        End Property

        Public Property Left() As Integer
            Get
                Return _left
            End Get
            Set(ByVal value As Integer)
                _left = value
            End Set
        End Property

        Public Property Top() As Integer
            Get
                Return _top
            End Get
            Set(ByVal value As Integer)
                _top = value
            End Set
        End Property

        Public Shadows Property Width() As Integer
            Get
                Return _width
            End Get
            Set(ByVal value As Integer)
                _width = value
            End Set
        End Property

#End Region

    End Class

End Namespace